package com.itheima;

import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result ex(SQLIntegrityConstraintViolationException e) {//方法形参中指定能够处理的异常类型
        log.info("出现异常：{}", e.getMessage());
        if (e.getMessage().contains("Duplicate entry")) {
            String[] split = e.getMessage().split(" ");
            String msg = split[2] + "已存在";
            return Result.error(msg);
        }
        return Result.error("你的网络异常 请重新操作");
    }
    @ExceptionHandler(Exception.class)
    public Result exceptionHandler(Exception e) {
        log.error("出现异常：{}", e.getMessage());
        return Result.error("你的网络异常 请重新操作");
    }
}
